
cap program drop lee_bounds_trimming_ado
program define lee_bounds_trimming_ado
syntax varlist (max = 1),  treatments(varlist) controls(varlist max = 1) attritvar(varname) DIRECtion(name)  [condcontrols(varlist) condfe(varlist) roundvar(varname) round(numlist max = 1)]

  if "`direction'"=="plus" {
    local signubt
    local signubc  -
    local signlbt  -
    local signlbc
  }

 if "`direction'"=="minus" {
    local signubt  -
    local signubc
    local signlbt
    local signlbc  -
  }

if "`direction'"!="minus" & "`direction'"!="plus" {
     di as error "You have to specify one from option: minus or plus"
     di as error "No other options are permited"
       error 197
       exit
}


  if "`roundvar'"!="" local roundwaveif   if `roundvar'==`round'
  if "`roundvar'"!="" local roundwaveand  &  `roundvar'==`round'

  if "`condcontrols'"=="" local outcomevar `varlist'
  if "`condcontrols'"!=""{
    if "`condfe'"!="" local vfe abs(`condfe')
    if "`condfe'"=="" local vfe noa

    qui reghdfe `varlist' `condcontrols', `vfe' resid
      tempvar `varlist'_temp
      predict ``varlist'_temp', residuals
      local outcomevar ``varlist'_temp'
      local csuffix     _c
  }

   /*Defining trimmed variables*/
  **********************************************************************
  cap drop `varlist'_t_ub`csuffix'
  gen `varlist'_t_ub`csuffix' = `varlist'
  label var `varlist'_t_ub`csuffix' "`:var label `varlist'' (Upper)"

  cap drop `varlist'_t_lb`csuffix'
  gen `varlist'_t_lb`csuffix' = `varlist'
  label var `varlist'_t_lb`csuffix' "`:var label `varlist'' (Lower)"

 /*Calculating attrition percentages per treatment arm*/
  **********************************************************************
  local perc_found
   qui count `roundwaveif'
    local fulln = r(N)

  local loops = 1
  foreach t in `treatments' `controls' {
  qui  count if `t'==1 `roundwaveand'
      local `t'_n = r(N)

  if `loops' ==1 local treat_n = ``t'_n'
  if `loops' > 1 local treat_n = `treat_n' + ``t'_n'

  qui  sum `attritvar' if `t'==1 `roundwaveand'
       local perc_found_`t'= 1 - r(mean)

  display `"Treatment: `: variable label `t''  found  `= round(`perc_found_`t'' * ``t'_n')' out of ``t'_n' "'  ///
          `"Percent found for round `round': `= round(`perc_found_`t'' * 100,.01)' "'

   if `loops' ==1 local perc_found `perc_found' `perc_found_`t''
   if `loops' > 1 local perc_found `perc_found', `perc_found_`t''


  local ++loops
  }

   local treatment_filter =  `treat_n' - `fulln'
       if `treatment_filter' != 0 {
         di as error "Treatment are do not fully constitude sample"
         di as error "Please check treatment and control assignment variables"
       error 197
       exit
     }


   /*Who is the minimum found*/
  **********************************************************************
    local minimum_found = min(`perc_found')

    local least_arm
   foreach t in `treatments' `controls' {
     if `perc_found_`t''==`minimum_found' local least_arm `least_arm' `t'
    }


   /*If only one treatment found, adjust variable*/
  **********************************************************************
 /*If more than one treatment found, bye bye*/
if `: word count `least_arm''>1 {
   di as error "there is a tie. Re-think your strategy"
     error 197
     exit
     }

if `: word count `least_arm''==1{

    display "Treatment group with lowest found round `round': `least_arm'"

     ds `treatments' `controls', not(varl `"`: variable label `least_arm''"')

       foreach t in `r(varlist)' {
           local drop_amount_`t' = -1 * round((`perc_found_`least_arm'' - `perc_found_`t'') * ``t'_n')

           display `"Dropping `drop_amount_`t'' for `: variable label `t'' for round `round'"'

           if "`t'"=="`controls'" gsort `t' `roundvar' `signubc'`outcomevar'
           if "`t'"!="`controls'" gsort `t' `roundvar' `signubt'`outcomevar'
           by `t' `roundvar': replace `varlist'_t_ub`csuffix' = . if _n <= `drop_amount_`t'' & `t' == 1 `roundwaveand' & !missing(`varlist')

           if "`t'"=="`controls'" gsort `t' `roundvar' `signlbc'`outcomevar'
           if "`t'"!="`controls'" gsort `t' `roundvar' `signlbt'`outcomevar'
           by `t' `roundvar': replace `varlist'_t_lb`csuffix' = . if _n <= `drop_amount_`t'' & `t' == 1 `roundwaveand' & !missing(`varlist')

        }
}

cap drop ``varlist'_temp' __000000
 end
